<?php

namespace App\Http\Controllers\Admin;

use App\Model\AdminUser;
use App\Model\System_basic;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    public function welcome()
    {
        //php获取今日开始时间戳和结束时间戳
        $today_start=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $today_end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        //php获取昨日起始时间戳和结束时间戳
        $yesterday_start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $yesterday_end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

        //php获取本周周起始时间戳和结束时间戳
        $thisweek_start=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
        $thisweek_end=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));

        //php获取本月起始时间戳和结束时间戳
        $thismonth_start=mktime(0,0,0,date('m'),1,date('Y'));
        $thismonth_end=mktime(23,59,59,date('m'),date('t'),date('Y'));

        //基地数据统计
        $spot_today = array();
        $spot_yesterday = array();
        $spot_thisweek = array();
        $spot_thismonth = array();
        $spot_today_total = 0;
        $yesterday_total = 0;
        $thisweek_total = 0;
        $thismonth_total = 0;
        $spot_time = DB::table('spots')->pluck('created_at');
        //基地总数
        $spot_total = count($spot_time);
        foreach ($spot_time as $kay => $value){
            $timestamp = strtotime($value);
            //基地今日数据统计
            if ($today_start < $timestamp && $today_end > $timestamp){
                $spot_today[] = $timestamp;
                $spot_today_total = count($spot_today);
            }
            //基地昨日数据统计
            if ($yesterday_start < $timestamp && $yesterday_end > $timestamp){
                $spot_yesterday[] = $timestamp;
                $yesterday_total = count($spot_yesterday);
            }
            //基地本周数据统计
            if ($thisweek_start < $timestamp && $thisweek_end > $timestamp){
                $spot_thisweek[] = $timestamp;
                $thisweek_total = count($spot_thisweek);
            }

            //基地本月数据统计
            if ($thismonth_start < $timestamp && $thismonth_end > $timestamp){
                $spot_thismonth[] = $timestamp;
                $thismonth_total = count($spot_thismonth);
            }
        }

        //资讯库数据统计
        $article_today = array();
        $article_yesterday = array();
        $article_thisweek = array();
        $article_thismonth = array();
        $article_today_total = 0;
        $yesterday_article_total = 0;
        $thisweek_article_total = 0;
        $thismonth_article_total = 0;
        $article_time = DB::table('article')->pluck('created_at');
        //资讯库总数
        $article_total = count($article_time);
        foreach ($article_time as $kay => $value){
            $timestamp = strtotime($value);
            //资讯库今日数据统计
            if ($today_start < $timestamp && $today_end > $timestamp){
                $article_today[] = $timestamp;
                $article_today_total = count($article_today);
            }
            //资讯库昨日数据统计
            if ($yesterday_start < $timestamp && $yesterday_end > $timestamp){
                $article_yesterday[] = $timestamp;
                $yesterday_article_total = count($article_yesterday);
            }
            //资讯库本周数据统计
            if ($thisweek_start < $timestamp && $thisweek_end > $timestamp){
                $article_thisweek[] = $timestamp;
                $thisweek_article_total = count($article_thisweek);
            }

            //资讯库本月数据统计
            if ($thismonth_start < $timestamp && $thismonth_end > $timestamp){
                $article_thismonth[] = $timestamp;
                $thismonth_article_total = count($article_thismonth);
            }
        }

        //游记库数据统计
        $travels_today = array();
        $travels_yesterday = array();
        $travels_thisweek = array();
        $travels_thismonth = array();
        $travels_today_total = 0;
        $yesterday_travels_total = 0;
        $thisweek_travels_total = 0;
        $thismonth_travels_total = 0;
        $travels_time = DB::table('travels')->pluck('created_at');
        //游记库总数
        $travels_total = count($travels_time);
        foreach ($travels_time as $kay => $value){
            $timestamp = strtotime($value);
            //游记库今日数据统计
            if ($today_start < $timestamp && $today_end > $timestamp){
                $travels_today[] = $timestamp;
                $travels_today_total = count($travels_today);
            }
            //游记库昨日数据统计
            if ($yesterday_start < $timestamp && $yesterday_end > $timestamp){
                $travels_yesterday[] = $timestamp;
                $yesterday_travels_total = count($travels_yesterday);
            }
            //游记库本周数据统计
            if ($thisweek_start < $timestamp && $thisweek_end > $timestamp){
                $travels_thisweek[] = $timestamp;
                $thisweek_travels_total = count($travels_thisweek);
            }

            //游记库本月数据统计
            if ($thismonth_start < $timestamp && $thismonth_end > $timestamp){
                $travels_thismonth[] = $timestamp;
                $thismonth_travels_total = count($travels_thismonth);
            }
        }

        //游记库数据统计
        $user_today = array();
        $user_yesterday = array();
        $user_thisweek = array();
        $user_thismonth = array();
        $user_today_total = 0;
        $yesterday_user_total = 0;
        $thisweek_user_total = 0;
        $thismonth_user_total = 0;
        $user_time = DB::table('users')->pluck('created_at');
        //游记库总数
        $user_total = count($user_time);
        foreach ($user_time as $kay => $value){
            $timestamp = strtotime($value);
            //游记库今日数据统计
            if ($today_start < $timestamp && $today_end > $timestamp){
                $user_today[] = $timestamp;
                $user_today_total = count($user_today);
            }
            //游记库昨日数据统计
            if ($yesterday_start < $timestamp && $yesterday_end > $timestamp){
                $user_yesterday[] = $timestamp;
                $yesterday_user_total = count($user_yesterday);
            }
            //游记库本周数据统计
            if ($thisweek_start < $timestamp && $thisweek_end > $timestamp){
                $user_thisweek[] = $timestamp;
                $thisweek_user_total = count($user_thisweek);
            }

            //游记库本月数据统计
            if ($thismonth_start < $timestamp && $thismonth_end > $timestamp){
                $user_thismonth[] = $timestamp;
                $thismonth_user_total = count($user_thismonth);
            }
        }

        //服务器信息
        //服务器ip地址
        $ip = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
        //服务器域名
        $server_name = $_SERVER['SERVER_NAME'];
        //服务器端口
        $server_port = $_SERVER["SERVER_PORT"];
        //本文件所在文件夹
        $document_root = $_SERVER['DOCUMENT_ROOT'];
        //服务器脚本超时时间
        $max_time = ini_get("max_execution_time");
        //服务器运行时间
        explode(",", exec('uptime'));

        return view('admin.index.welcome',compact('spot_total','spot_today_total','yesterday_total','thisweek_total','thismonth_total',
            'article_total','article_today_total','yesterday_article_total','thisweek_article_total','thismonth_article_total','travels_total','travels_today_total',
            'yesterday_travels_total','thisweek_travels_total','thismonth_travels_total','user_total','user_today_total','yesterday_user_total','thisweek_user_total',
            'thismonth_user_total','ip','server_name','server_port','document_root','max_time'));
    }
    public function index()
    {
        $system = System_basic::orderBy('id','desc')->first();
        $admin_user = AdminUser::orderBy('id','desc')->first();
        return view('admin.index.index',compact('system','admin_user'));
    }
}
